package com.ruitu.mall_98_1.product.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.base.BaseModel;
import com.beanu.arad.base.BasePresenter;
import com.beanu.arad.base.my_base.MyRefreshBaseGridActivity;
import com.beanu.arad.utils.UIUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.product.adapter.ProductListAdapter;
import com.ruitu.router_module.bean.product.Product;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public abstract class BaseProListActivity<T extends BasePresenter, E extends BaseModel> extends MyRefreshBaseGridActivity<T, E> {
    protected View empty_view;
    protected RelativeLayout fl_sort, fl_num, fl_price;
    //    protected ImageView iv_num, iv_price;//销量排序,价格排序
    protected ImageView iv_ur_num, iv_dr_num, iv_ur_price, iv_dr_price;
    protected FrameLayout fl_icon;//返回图标
    protected TextView tv_search;//右上角的搜索/取消(模块列表的时候显示)
    protected EditText et_keys;//输入关键字

    protected int currPage = 1;//当前页码
    protected int totalPage = 1;//数据总页数
    protected int totalNum = 1;//数据总条数

    protected ProductListAdapter productAdapter;
    protected List<Product> productList = new ArrayList<>();//商品列表

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        empty_view = findViewById(R.id.empty_view);
        lRecyclerView.setEmptyView(empty_view);
        fl_sort = findViewById(R.id.fl_sort);
        fl_num = findViewById(R.id.fl_num);
        fl_price = findViewById(R.id.fl_price);
//        iv_num = findViewById(R.id.iv_num);
//        iv_price = findViewById(R.id.iv_price);
        fl_icon = findViewById(R.id.fl_icon);
        tv_search = findViewById(R.id.tv_search);
        et_keys = findViewById(R.id.et_keys);
        iv_ur_num = findViewById(R.id.iv_ur_num);
        iv_dr_num = findViewById(R.id.iv_dr_num);
        iv_ur_price = findViewById(R.id.iv_ur_price);
        iv_dr_price = findViewById(R.id.iv_dr_price);

        try {
            flList.add(fl_sort);
            flList.add(fl_num);
            flList.add(fl_price);
            fl_sort.setOnClickListener(this);
            fl_num.setOnClickListener(this);
            fl_price.setOnClickListener(this);
            fl_icon.setOnClickListener(this);
        } catch (Exception e) {
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == fl_icon) {//返回图标
            if (null != et_keys) {
                UIUtils.hideInput(this, et_keys.getWindowToken());
            }
            onBackPressed();
        }
        if (v == fl_sort) {//点击综合
            setState(fl_sort, !fl_sort.isSelected());
            if (fl_sort.isSelected()) {
                setSelectState(fl_sort);
            }
            setFilterState(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
            reqFirst();
            showProgress();
        }
        if (v == fl_num) {//按销量
            setState(fl_num, !fl_num.isSelected());
            if (fl_num.isSelected()) {
                setSelectState(fl_num);
            }
            if (fl_num.isSelected()) {//销量升序排列
                sortProList(true, false);
                setFilterState(View.VISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
            } else {//销量降序排列
                sortProList(false, false);
                setFilterState(View.INVISIBLE, View.VISIBLE, View.INVISIBLE, View.INVISIBLE);
            }
        }
        if (v == fl_price) {//按价格
            setState(fl_price, !fl_price.isSelected());
            if (fl_price.isSelected()) {
                setSelectState(fl_price);
            }
            if (fl_price.isSelected()) {//价格升序排列
                sortProList(true, true);
                setFilterState(View.INVISIBLE, View.INVISIBLE, View.VISIBLE, View.INVISIBLE);
            } else {//价格降序排列
                sortProList(false, true);
                setFilterState(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.VISIBLE);
            }
        }
    }

    private void setState(View v, boolean isSelected) {
        v.setSelected(isSelected);
    }

    private List<RelativeLayout> flList = new ArrayList<>();

    protected void setSelectState(RelativeLayout fl) {
        for (RelativeLayout f : flList) {
            if (f == fl) {
                f.setSelected(true);
            } else {
                f.setSelected(false);
            }
        }
    }

    private void sortProList(final boolean isUp, final boolean isPrice) {
        if (!isDataEmpty(productList)) {
            Collections.sort(productList, new Comparator<Product>() {
                @Override
                public int compare(Product p1, Product p2) {
                    try {
                        if (isPrice) {//根据价格排序
                            try {
                                if (isUp) {//升序排列
                                    return p1.getGoodMarket() > p2.getGoodMarket() ? 1 : -1;
                                } else {
                                    return p2.getGoodMarket() > p1.getGoodMarket() ? 1 : -1;
                                }
                            } catch (Exception e) {//商品价格异常
                                return 1;
                            }
                        } else {//根据销量排序
                            if (isUp) {//升序排列
                                return p1.getGoodVolume() > p2.getGoodVolume() ? 1 : -1;
                            } else {
                                return p2.getGoodVolume() > p1.getGoodVolume() ? 1 : -1;
                            }
                        }
                    } catch (Exception e) {
                        return -1;
                    }
                }
            });
            productAdapter.setDataList(productList);
        }
    }

    //设置筛选状态
    protected void setFilterState(int v1, int v2, int v3, int v4) {
        iv_ur_num.setVisibility(v1);
        iv_dr_num.setVisibility(v2);
        iv_ur_price.setVisibility(v3);
        iv_dr_price.setVisibility(v4);
    }

    @Override
    public void hideProgress() {
        super.hideProgress();
        refreshComplete();
    }

    @Override
    protected abstract int getLayoutRes();

    @Override
    protected abstract RecyclerView.Adapter initAdapter();

    @Override
    protected abstract LRecyclerView findRecyclerView();

    @Override
    protected abstract void reqFirst();

    @Override
    protected abstract void reqMore();

    @Override
    protected void reReq() {
    }

    @Override
    protected void reqData() {
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
